﻿using ConfExpert.Forms.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevTeam;
using ConfExpert.Utility;
using DevExpress.XtraEditors.Repository;

namespace ConfExpert.Forms.Depo.Kauf
{
    public partial class BOMEsleme : XForm
    {
        public int CustomerId { get; set; }
        public BOMEsleme(int CustomerId)
        {
            InitializeComponent();
            this.CustomerId = CustomerId;
        }

        private void BOMEsleme_Load(object sender, EventArgs e)
        {
            gvBOMLines.Init("GetBOMEsleme",CustomerId);

            string BrandIds = "";
            foreach (DataRow dr in AppSession.Lookups.Brand.Select("CustomerId=" + CustomerId))
                BrandIds += dr["BrandId"].ToStr() + ",";
            BrandIds += "1";
            
            DataView clone = new DataView(AppSession.Lookups.V_Aksesuar);
            clone.RowFilter = "BrandId in (" + BrandIds + ")";
            try
            {
                if (clone.Count > 0)
                {
                    RepositoryItemGridLookUpEdit rep = RI(Rp.V_Aksesuar).Clone() as RepositoryItemGridLookUpEdit;
                    rep.DataSource = clone;
                    gvBOMLines.Columns["AksesuarId"].ColumnEdit = rep;
                }
            }
            catch
            {
                
            }

            gvBOMLines.MakeReadOnly("AksesuarId");

            bbiRefresh.ItemClick += bbiRefresh_ItemClick;
            bbiSave.ItemClick += bbiSave_ItemClick;

            gvBOMLines.AddSummary("RequiredQty");

            gvBOMLines.BestFitColumns();
        }

        void bbiSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvBOMLines.EndEditing();
            string xml = gvBOMLines.SerializeChanges();
            ExecuteNonQuery("UpdBOMEsleme", xml, CustomerId, WhoAmI);
        }

        void bbiRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvBOMLines.RefreshDataSource("GetBOMEsleme", CustomerId);
        }

    }
}
